﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using HalalCheck.DataModels;
using HalalCheck.Services;
using HalalCheck.Utils.HttpClient;
using Newtonsoft.Json;
using Xamarin;
using Xamarin.Forms;

[assembly: Dependency(typeof(SalahServices))]
namespace HalalCheck.Services
{
    public class SalahServices : ISalahServices
    {

        public async Task<DeviceLocation> GetLocation()
        {
            HttpResponseMessage res =
                await WebClient.GetClient().GetAsync("http://api.hostip.info/get_json.php?method=5");
            res.EnsureSuccessStatusCode();
            string json = await res.Content.ReadAsStringAsync();
            DeviceLocation location = ((DeviceLocation)JsonConvert.DeserializeObject(json, typeof(DeviceLocation)));
            return location;
        }

        public async Task<SalatData> GetSalatData()
        {
            SalatData salatData = null;
            try
            {
                HttpResponseMessage res =
                    await
                        WebClient.GetClient()
                            .GetAsync("http://muslimsalat.com/daily.json?key=caa468d8f81ee260e098fc1b07862e76");
                res.EnsureSuccessStatusCode();
                string json = await res.Content.ReadAsStringAsync();
                salatData = ((SalatData) JsonConvert.DeserializeObject(json, typeof (SalatData)));
            }
            catch (Exception exception)
            {
                Insights.Report(exception);
            }
            finally
            {
             
            }

            return salatData;  
        }
    }
}
